package top.leoseven.adminproject.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.StuHomeWorkInfo;
import top.leoseven.adminproject.modle.ToastUtils;
import top.leoseven.adminproject.presenter.StuHomeWorkPresenter;
import top.leoseven.adminproject.service.StuHomeWorkService;
import top.leoseven.adminproject.view.adapter.StuHomeWorkAdapter;

public class StuHomeWorkList extends BaseActivity implements StuHomeWorkService {
    private int type=-1;
    public static final int selectByStuID=1001;
    public static final int selectByKechengID=1002;
    public static final int selectByHomeWorkID=1003;
    private RecyclerView list;
    private StuHomeWorkPresenter shwp;
    private ProgressDialog dialog;
    private List<StuHomeWorkInfo> datas;
    private StuHomeWorkAdapter adapter;

    private void initView() {
        list= (RecyclerView) findViewById(R.id.recycle);
        dialog=new ProgressDialog(this);
        dialog.setMessage("正在加载数据");
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog!=null) {
            dialog.cancel();
        }
    }

    @Override
    public void setLayout() {
        setContentView(R.layout.activity_stu_home_work_list);
        initView();

    }

    @Override
    public void initPresenter() {
        datas=new ArrayList<>();
        adapter = new StuHomeWorkAdapter(this, datas);
        list.setAdapter(adapter);
        list.setLayoutManager(new LinearLayoutManager(this));
        shwp=new StuHomeWorkPresenter();
        shwp.init(this,this);
        Intent it = getIntent();
        int type = it.getIntExtra("type", -1);
        if (type==selectByKechengID) {
            int kcid = it.getIntExtra("kcid", -1);
            shwp.load(kcid,selectByKechengID);
        }else if (type==selectByHomeWorkID){
            int workid = it.getIntExtra("workid", -1);
            shwp.load(workid,selectByHomeWorkID);
        }else if (type==selectByStuID){
            String stuid = it.getStringExtra("stuid");
            shwp.load(stuid);
        }
    }

    @Override
    public void onSuccess(List<StuHomeWorkInfo> list) {
        dialog.dismiss();
        if (list.size()==0) {
            ToastUtils.toast(ToastUtils.CAHAN,"未查到任何作业数据",this, Toast.LENGTH_SHORT);
            finish();
        }else{
            datas.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onError(String err) {
        dialog.dismiss();
    }
}
